﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class KhachHangDAO:AbstractDAO
    {
        public static bool them(KhachHangDTO khach, long maLoaiKhach)
        {
            bool ketQua = true;
            OleDbConnection ketNoi = null;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "INSERT INTO KhachHang(TenKhachHang,MaLoaiKhach,CMND,DiaChi)";
                chuoiLenh += " VALUES (@TenKhachHang,MaLoaiKhach,@CMND,@DiaChi)";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo;
                thamSo = new OleDbParameter("@TenKhachHang", OleDbType.VarChar);
                thamSo.Value = khach.TenKhachHang;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@MaLoaiKhach", OleDbType.Integer);
                thamSo.Value = khach.LoaiKhach.MaLoaiKhach;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@CMND", OleDbType.VarChar);
                thamSo.Value = khach.CMND;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@DiaChi", OleDbType.VarChar);
                thamSo.Value = khach.DiaChi;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketQua = false;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;
        }
        public static long layMaKhachLonNhat()
        {
            OleDbConnection ketNoi = null;
            long maKhachHangMax=0;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT MAX(MaKhachHang) FROM KhachHang ";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbDataReader boDoc = lenh.ExecuteReader();
                while (boDoc.Read())
                {
                     maKhachHangMax= boDoc.GetInt32(0);

                }

            }
            catch
            {
                
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return maKhachHangMax;
        }
        public static bool xoa(long maKhachHang)
        {
            bool ketQua = true;
            OleDbConnection ketNoi = null;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "DELETE FROM KhachHang WHERE MaKhachHang=@MaKhachHang";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo;
                thamSo = new OleDbParameter("@MaKhachHang", OleDbType.Integer);
                thamSo.Value = maKhachHang;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketQua = false;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;
        }
        public static bool capNhat(KhachHangDTO khach)
        {
            bool ketQua = true;
            OleDbConnection ketNoi = null;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "UPDATE KhachHang SET TenKhachHang=@TenKhachHang,MaLoaiKhach=@MaLoaiKhach,CMND=@CMND,DiaChi=@DiaChi WHERE MaKhachHang=@MaKhachHang";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);
                OleDbParameter thamSo;

                thamSo = new OleDbParameter("@TenKhachHang", OleDbType.VarChar);
                thamSo.Value = khach.TenKhachHang;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@MaLoaiKhach", OleDbType.Integer);
                thamSo.Value = khach.LoaiKhach.MaLoaiKhach;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@CMND", OleDbType.VarChar);
                thamSo.Value = khach.CMND;
                lenh.Parameters.Add(thamSo);
                
                thamSo = new OleDbParameter("@DiaChi", OleDbType.VarChar);
                thamSo.Value = khach.DiaChi;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@MaKhachHang", OleDbType.Integer);
                thamSo.Value = khach.MaKhachHang;
                lenh.Parameters.Add(thamSo);


                lenh.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                ketQua = false;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;

        }
    }
}
